<template>
  <div class="page-category">
    <nav-bar title="分类"></nav-bar>

    <van-tabs v-model="active" @click="getCategoryGoodsList">
      <van-tab
        :title="item.name"
        v-for="item in navList"
        :key="item.id"
      ></van-tab>
    </van-tabs>

    <div class="goods-list-holder">
      <div class="title-holder">
        <p class="title">{{ navObj.name }}</p>
        <p class="sub-title">{{ navObj.front_desc }}</p>
      </div>

      <ul class="list">
        <li
          class="item"
          v-for="item in goodsList"
          :key="item.id"
          @click="toGoods(item.id)"
        >
          <img :src="item.list_pic_url" alt="" />
          <p class="title">{{ item.name }}</p>
          <p class="price">￥{{ item.retail_price }}</p>
        </li>
      </ul>

      <div class="null-show" v-if="nullShow">
        数据库暂无数据
      </div>
    </div>
  </div>
</template>

<script>
import { getPageCategoryNav, getPageGoodsList } from "@/api/pages/Category";
import NavBar from "@/components/common/nav-bar";
export default {
  data() {
    return {
      active: 0,
      id: this.$route.query.id,
      navList: [],
      navObj: {},
      goodsList: [],
      nullShow:true,
    };
  },
  methods: {
    // 获取物品列表
    getCategoryGoodsList(index) {
      getPageGoodsList({ categoryId: this.navList[index].id }).then((res) => {
        console.log(res);
        this.navObj = res.currentNav;
        this.goodsList = res.data;
        this.nullShow = this.goodsList.length === 0
      });
    },
    // 跳转到商品界面
    toGoods(id) {
      this.$router.push({ path: "/pages/goods/goods", query: { id: id } });
    },
  },
  created() {
    // 获取标签列表
    getPageCategoryNav({ id: this.id }).then((res) => {
      this.navList = res.navData;
      for (let i = 0; i < this.navList.length; i++) {
        if (this.id == this.navList[i].id) {
          this.active = i;
          this.getCategoryGoodsList(i);
        }
      }
    });
  },
  components: {
    "nav-bar": NavBar,
  },
};
</script>

<style lang="scss">
.page-category {
  .goods-list-holder {
    margin-top: 10px;
    width: 100%;
    .title-holder {
      background-color: #fff;
      box-sizing: border-box;
      padding: 15px;
      height: 100px;
      text-align: center;
      .title {
        margin-top: 15px;
        font-size: 14px;
      }
      .sub-title {
        margin-top: 12px;
        color: #999;
      }
    }
    .list {
      overflow: hidden;
      .item {
        float: left;
        width: 50%;
        height: 225px;
        border: 1px solid #d9d9d9;
        text-align: center;
        box-sizing: border-box;
        background: #fff;
        img {
          display: block;
          margin: 0 auto;
          width: 150px;
          height: 150px;
        }
        .price {
          margin-top: 15px;
          font-size: 14px;
          color: #b4282d;
        }
      }
    }
    .null-show{
      width: 100%;
      height: 100px;
      line-height: 100px;
      text-align: center;
      color: #999;
    }
  }
}
</style>